package com.cskaoyan.service;

import com.cskaoyan.bean.DataDTO;
import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.RegionExample;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: ${description}
 * @author: cuicui
 **/
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    public DataDTO regionList(){
        //{"total":31,"pages":1,"limit":31,"page":1
        DataDTO dataDTO = new DataDTO();
        //查询所有省份（pid=0）
        List<Region> regions = RegionServiceImpl.getRegions(Constant.PROVINCES_PID, regionMapper);

        int total = regions.size();
        dataDTO.setTotal(total);
        Integer limit = Constant.REGION_LIMIT;
        dataDTO.setLimit(limit);
        int pages = (int) Math.ceil((total * 1.0 / limit));
        dataDTO.setPages(pages);

        List<Region> regions1;// 市级信息
        List<Region> regions2; //县级信息
        ArrayList<DataDTO.ListDTO> listDTOS = new ArrayList<>();
        for (int i = 0; i < total; i++) {
            dataDTO.setPage((i/limit+1));
            Region region = regions.get(i);
            DataDTO.ListDTO listDTO = new DataDTO.ListDTO();
            listDTO.setId(region.getId());
            listDTO.setName(region.getName());
            listDTO.setCode(region.getCode());
            listDTO.setType((int)region.getType());
            //查询市（市pid=省id）
            regions1 = RegionServiceImpl.getRegions(region.getId(), regionMapper);
            ArrayList<DataDTO.ListDTO.ChildrenDTO> childrenDTOS = new ArrayList<>();
            for (Region region1 : regions1) {
                DataDTO.ListDTO.ChildrenDTO childrenDTO = new DataDTO.ListDTO.ChildrenDTO();
                childrenDTO.setId(region1.getId());
                childrenDTO.setName(region1.getName());
                childrenDTO.setCode(region1.getCode());
                childrenDTO.setType((int)region1.getType());
                //查询县区（县区pid=市id）
                regions2 = RegionServiceImpl.getRegions(region1.getId(), regionMapper);
                ArrayList<DataDTO.ListDTO.ChildrenDTO.ChildrenDTO2> childrenDTO2s = new ArrayList<>();
                for (Region region2 : regions2) {
                    //封装县区信息
                    DataDTO.ListDTO.ChildrenDTO.ChildrenDTO2 childrenDTO2 = new DataDTO.ListDTO.ChildrenDTO.ChildrenDTO2();
                    childrenDTO2.setId(region2.getId());
                    childrenDTO2.setName(region2.getName());
                    childrenDTO2.setCode(region2.getCode());
                    childrenDTO2.setType((int)region2.getType());
                    childrenDTO2s.add(childrenDTO2);
                }
                childrenDTO.setChildren(childrenDTO2s);
                childrenDTOS.add(childrenDTO);
            }
            listDTO.setChildren(childrenDTOS);
            listDTOS.add(listDTO);
        }
        dataDTO.setList(listDTOS);
        return dataDTO;

    }

    //根据pid查询region信息
    private static List<Region> getRegions(Integer pid,RegionMapper regionMapper) {
        RegionExample regionExample = new RegionExample();
        regionExample.createCriteria().andPidEqualTo(pid);
        return regionMapper.selectByExample(regionExample);
    }

}
